package com.mall.shop.controller;

import com.mall.shop.entity.product.Category;
import com.mall.shop.service.CategoryService;
import com.mall.shop.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

;

/**
 * 商品三级分类()表控制层
 *
 * @author 龙茶清欢
 * @since 2023-01-16 15:32:39
 */
@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
@Tag(name = "商品三级分类管理", description = "商品三级分类管理")
public class CategoryController {
  
    private final CategoryService categoryService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("/findListWithTree")
    @Operation(description = "通过分页查询")
    public R<Object> findListWithTree() {
        List<Category> categories = categoryService.findListWithTree();
        return R.ok(categories);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/findById/{id}")
    @Operation(description = "通过ID查询")
    public R<Object> findById(@PathVariable Long id) {
        return R.ok(categoryService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param category 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @Operation(description = "新增")
    public R<Object> insert(@RequestBody Category category) {
        return R.ok(categoryService.save(category));
    }

    /**
     * 修改数据
     *
     * @param category 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(description = "更新")
    public R<Object> update(@RequestBody Category category) {
        return R.ok(categoryService.updateById(category));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(description = "删除")
    public R<Object> delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(categoryService.removeByIds(idList));
    }

    @GetMapping("/test")
    @Operation(description = "压力测试")
    public R<Object> test() {
        return R.ok("响应成功");
    }
}

